<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('问卷调查') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('客户类型')" prop="investType">
        <el-select v-model="queryParams.investType" :placeholder="$t('客户类型')" clearable>
          <el-option
            v-for="dict in investTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('客户名称')" prop="investId">
        <el-select v-model="queryParams.investId" :placeholder="$t('客户名称')" clearable @keyup.enter.native="queryHandle">
          <el-option
            v-for="dict in lpBaseOption"
            :key="dict.name"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('问卷名称')" prop="questionName">
        <el-input
          v-model="queryParams.questionName"
          :placeholder="$t('问卷名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('对应结果')" prop="result">
        <el-select v-model="queryParams.result" :placeholder="$t('对应结果')" clearable @keyup.enter.native="queryHandle">
          <el-option
            v-for="dict in correspondingResults"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('状态')" prop="status">
        <el-select v-model="queryParams.status" :placeholder="$t('状态')" clearable @keyup.enter.native="queryHandle">
          <el-option :label="$t('未答题')" value="未答题" />
          <el-option :label="$t('已答题')" value="已答题" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['lp:investigation:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}
        </el-button>
      </el-col>
      -->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          v-hasPermi="['lp:investigation:export']"-->
    <!--          type="warning"-->
    <!--          icon="el-icon-download"-->
    <!--          size="mini"-->
    <!--          @click="exportHandle"-->
    <!--        >{{ $t('导出') }}-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="lpQuestionnaireInvestigationList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('客户类型')" align="center" prop="investType" :formatter="investTypeFormat" width="80" />
      <el-table-column :label="$t('客户名称')" align="center" prop="investName" :min-width="200" />
      <el-table-column :label="$t('问卷类型')" align="center" prop="type" :min-width="150" :formatter="questionnaireTypeFormat" />
      <el-table-column :label="$t('问卷名称')" align="center" prop="questionName" :min-width="150" />
      <el-table-column :label="$t('总分')" align="center" prop="score" :min-width="80" />
      <el-table-column :label="$t('对应结果')" align="center" prop="result" :min-width="100" :formatter="resultsFormat" />
      <el-table-column :label="$t('答题时间')" align="center" prop="time" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('创建时间')" align="center" prop="createTime" :min-width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('答题状态')" align="center" prop="status" fixed="right" :min-width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.status === '已答题'" style="color: forestgreen">{{ scope.row.status }}</span>
          <span v-else>{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status !== '已答题'"
            v-hasPermi="['lp:investigation:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="startAnsweringQuestions(scope.row, true)"
          >{{ $t('答题') }}</el-button>
          <el-button
            v-if="scope.row.status === '已答题'"
            v-hasPermi="['lp:investigation:edit']"
            size="small"
            type="text"
            icon="el-icon-view"
            @click="startAnsweringQuestions(scope.row, false)"
          >{{ $t('查看') }}</el-button>
          <el-button
            v-if="scope.row.status !== '已答题'"
            v-hasPermi="['lp:investigation:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row, true)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-if="scope.row.status !== '已答题'"
            v-hasPermi="['lp:investigation:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改问卷调查对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" :destroy-on-close="true" custom-class="dialogForm" append-to-body>
      <lpQuestionnaireInvestigationForm ref="lpQuestionnaireInvestigationForm" />
    </el-dialog>

    <el-dialog :title="$t(title)" :visible.sync="problemOpen" :close-on-click-modal="false" :lock-scroll="false" :destroy-on-close="true" custom-class="dialogForm" append-to-body>
      <lpQuestionnaireProblem ref="lpQuestionnaireProblem" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listLpQuestionnaireInvestigation,
  delLpQuestionnaireInvestigation,
  exportLpQuestionnaireInvestigation
} from '@/api/fund/questionnaire/investigation/lpQuestionnaireInvestigation'
import { getLpBaseByType } from '@/api/fund/questionnaire/settings/questionnaireQuestion/lpQuestionnaireQuestion'
import lpQuestionnaireInvestigationForm from './lpQuestionnaireInvestigationForm.vue'
import lpQuestionnaireProblem from '@/views/fund/questionnaire/investigation/lpQuestionnaireProblem'

export default {
  components: {
    lpQuestionnaireInvestigationForm,
    lpQuestionnaireProblem
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      problemOpen: false,
      // 总条数
      total: 0,
      // 问卷调查表格数据
      lpQuestionnaireInvestigationList: [],
      // 弹出层标题
      title: '问卷调查',
      // 是否显示弹出层
      formOpen: false,
      // 客户类型字典
      investTypeOptions: [],
      questionnaireTypeOption: [],
      // 查询参数
      queryParams: {
        dataId: undefined,
        investType: undefined,
        investId: undefined,
        type: undefined,
        questionName: undefined,
        time: undefined,
        score: undefined,
        result: undefined,
        status: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      lpBaseOption: [],
      correspondingResults: []
    }
  },
  watch: {
    'queryParams.investType': {
      handler(val) {
        this.queryParams.investId = undefined
        getLpBaseByType(val).then(response => {
          this.lpBaseOption = response.data
        })
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('lp_type').then(response => {
      this.investTypeOptions = response.data
    })
    this.getDicts('questionnaire_type').then(response => {
      this.questionnaireTypeOption = response.data
    })
    this.getDicts('corresponding_results').then(response => {
      this.correspondingResults = response.data
    })
  },
  methods: {
    /** 查询问卷调查列表 */
    getList() {
      this.loading = true
      listLpQuestionnaireInvestigation(this.queryParams).then(response => {
        this.lpQuestionnaireInvestigationList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 客户类型字典翻译
    investTypeFormat(row) {
      return this.selectDictLabel(this.investTypeOptions, row.investType)
    },
    // 问卷类型
    questionnaireTypeFormat(row) {
      return this.selectDictLabel(this.questionnaireTypeOption, row.type)
    },
    // 对应结果
    resultsFormat(row) {
      return this.selectDictLabel(this.correspondingResults, row.result)
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dataId: undefined,
        investType: undefined,
        investId: undefined,
        type: undefined,
        questionNameId: undefined,
        time: undefined,
        score: undefined,
        result: undefined,
        status: '0',
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.investId = undefined
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.lpQuestionnaireInvestigationForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row, editStatus) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.lpQuestionnaireInvestigationForm.init(row, editStatus)
      })
    },
    /** 开始答题 */
    startAnsweringQuestions(row, editStatus) {
      this.problemOpen = true
      this.$nextTick(() => {
        this.$refs.lpQuestionnaireProblem.init(row, editStatus)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return delLpQuestionnaireInvestigation(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
        this.loading = false
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return exportLpQuestionnaireInvestigation(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(() => {
      })
    }
  }
}
</script>
